package com.tengju.bff.interfaces.manage.platform.message.facade;

import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import com.tengju.bff.interfaces.manage.platform.message.assembler.MessageAssembler;
import com.tengju.bff.interfaces.manage.platform.message.vo.GetMessageForm;
import com.tengju.bff.interfaces.manage.platform.message.vo.MessageContentVO;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.PageResultVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.support.application.message.MessageService;
import com.tengju.support.application.shared.ApplicationException;
import com.tengju.support.domain.model.message.MessageTask;
import com.tengju.support.domain.model.message.MessageTaskRepository;
import com.tengju.support.domain.model.message.TaskId;
import com.tengju.support.domain.model.message.MessageType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.tengju.support.application.shared.ApplicationExceptionCode.MESSAGE_NOT_EXIST;

/**
 * @author: 李大仙
 * @date: 2021/6/7 6:01 下午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MessageFacade {

    private final MessageService messageService;
    private final MessageTaskRepository messageContentRepository;

    public TableVO getByPage(GetMessageForm form) {
        Integer pageIndex = form.getPageIndex();
        Integer pageSize = form.getPageSize();
        Integer offset = form.getOffset();

        TableVO tableVO = new TableVO();
        tableVO.setHeader(List.of(
                ColumnVO.of("title", "消息标题", false),
                ColumnVO.of("createTime", "创建时间", false),
                ColumnVO.of("sendTime", "发送时间", false),
                ColumnVO.of("messageType", "类型", false),
                ColumnVO.of("sendStatusDesc", "状态", false),
                ColumnVO.of("updateBy", "操作人", false)
        ));
        List<MessageType> messageTypeList = List.of(MessageType.BULLETIN, MessageType.PRIVATE_MESSAGE, MessageType.SYSTEM_INFORMATION);
        int total = messageService.getTotal(form.getKeyword(), messageTypeList);
        PageResultVO page = PageResultVO.of(pageIndex, pageSize, total);
        tableVO.setPage(page);

        List<Map<String, Object>> records = new ArrayList<>();
        tableVO.setRecords(records);

        List<MessageTask> list = Lists.newArrayList();
        if (form.getOffset() <= total) {
            list = messageService.getPage(form.getKeyword(), messageTypeList, offset, pageSize);
        }
        if (CollectionUtils.isEmpty(list)) {
            return tableVO;
        }

        records = list.stream().map(m -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("title", m.getTitle());
            map.put("messageId", m.getTaskId().getValue());
            map.put("createTime", DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_MINUTE_FORMAT, m.getCreateTime()));
            map.put("sendTime", DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_MINUTE_FORMAT, m.getSendTime()));
            map.put("messageType", m.getMessageType().getDesc());
            map.put("sendStatus", m.getSendStatus().getVal());
            map.put("sendStatusDesc", m.getSendStatus().getDesc());
            map.put("updateBy", m.getUpdateBy());
            return map;
        }).collect(Collectors.toList());
        tableVO.setRecords(records);

        return tableVO;
    }

    public List<KVVO<String, Integer>> getStationLetterTypeList() {
        return MessageAssembler.getStationLetterTypeList();
    }

    public List<KVVO<String, Integer>> getMessageContentTypeList() {
        return MessageAssembler.getMessageContentTypeList();
    }

    public List<KVVO<String, Integer>> getMessageUserOrientationList() {
        return MessageAssembler.getMessageUserOrientationList();
    }

    public List<KVVO<String, Integer>> getMessageSendStatusList() {
        return MessageAssembler.getMessageSendStatusList();
    }

    public MessageContentVO detail(Long messageId) {
        MessageTask existMessageContent = messageContentRepository.get(new TaskId(messageId));
        if (existMessageContent == null) {
            throw ApplicationException.error(MESSAGE_NOT_EXIST, messageId);
        }
        return new MessageContentVO(
                messageId,
                existMessageContent.getTitle(),
                existMessageContent.getMessageType().getVal(),
                existMessageContent.getContentType().getVal(),
                existMessageContent.getImgUrl(),
                existMessageContent.getContent(),
                existMessageContent.getJumpUrl(),
                existMessageContent.getMessageUserOrientation().getOrientationType().getVal(),
                existMessageContent.getMessageUserOrientation().getOrientationObject(),
                existMessageContent.getPush(),
                existMessageContent.getSendStatus().getVal(),
                existMessageContent.getSendStatus().getDesc(),
                existMessageContent.getSendTime());
    }


}
